<?php
namespace TennisTournament\Helpers;

use TennisTournament\Model\Group;
use TennisTournament\Model\GroupStats;
use TennisTournament\Model\Match;
use TennisTournament\Model\Player;
use TennisTournament\Model\Result;
use TennisTournament\Model\Security;
use TennisTournament\Model\GameEnum;

final class ModelFactoriesHelper {
    private function __construct(){}

    public static function createSecurity($id, $login, $password, $role) {
        $security = new Security();

        if (isset($id)) {
            $security->setId($id);
        }
        if (isset($login)) {
            $security->setLogin($login);
        }
        if (isset($password)) {
            $security->setPassword($password);
        }
        if (isset($role)) {
            $security->setRole($role);
        }

        return $security;
    }

    public static function createPlayer($id, $name, $jobPosition, $pathToPicture,
            $numberWinners, $numberLosses, $obtainedPoints, $madePoints) {
        $player = new Player();
        if (isset($id)) {
            $player->setId($id);
        }
        if (isset($name)) {
            $player->setName($name);
        }
        if (isset($jobPosition)) {
            $player->setJobPosition($jobPosition);
        }
        if (isset($pathToPicture)) {
            $player->setPathToPicture($pathToPicture);
        }
        if (isset($numberWinners)) {
            $player->setNumberWinners($numberWinners);
        }
        if (isset($numberLosses)) {
            $player->setNumberLosses($numberLosses);
        }
        if (isset($obtainedPoints)) {
            $player->setObtainedPoints($obtainedPoints);
        }
        if (isset($madePoints)) {
            $player->setMadePoints($madePoints);
        }
        return $player;
    }

    public static function createGroupStats($id, $player, $takenPoints, $getPoints,
                                      $countVictories, $countLosses, $points) {
        $groupStats = new GroupStats();
        if (isset($id)) {
            $groupStats->setId($id);
        }
        if (isset($player)) {
            $groupStats->setPlayer($player);
        }
        if (isset($takenPoints)) {
            $groupStats->setTakenPoints($takenPoints);
        }
        if (isset($getPoints)) {
            $groupStats->setGetPoints($getPoints);
        }
        if (isset($countVictories)) {
            $groupStats->setCountVictories($countVictories);
        }
        if (isset($countLosses)) {
            $groupStats->setCountLosses($countLosses);
        }
        if (isset($points)) {
            $groupStats->setPoints($points);
        }
        return $groupStats;
    }

    public static function craeteGroup($name, $groupStats = array()) {
        $group = new Group();
        $group->setName($name);
        $group->setStats($groupStats);
        return $group;
    }

    public static function createResult($game, $hostPoints, $guestPoints) {
        $result = new Result();
        if (isset($game)) {
            $result->setGame($game);
        }
        if (isset($hostPoints)) {
            $result->setHostScore($hostPoints);
        }
        if (isset($guestPoints)) {
            $result->setGuestScore($guestPoints);
        }

        return $result;
    }

    public static function updateMatchBy($params = array(), Match $match) {
        if ($params['date'] != null) {
            $match->setDate(new \DateTime($params['date']));
        } else {
            $match->setDate(null);
        }

        if ($params['winner'] != null) {
            $match->setWinner($params['winner']);
        }

        $collectionResults = null;
        if ($params['hostScoreFGame'] != null) {
            $resultFirstGame = ModelFactoriesHelper::createResult(GameEnum::FIRST,
                (int)$params['hostScoreFGame'], (int)$params['guestScoreFGame']);
            $resultFirstGame->setMatch($match);

            $resultSecondGame = ModelFactoriesHelper::createResult(GameEnum::SECOND,
                (int)$params['hostScoreSGame'], (int)$params['guestScoreSGame']);
            $resultSecondGame->setMatch($match);

            $collectionResults = array($resultFirstGame, $resultSecondGame);
        }

        if ($params['hostScoreTGame'] != null) {
            $resultThirdGame = ModelFactoriesHelper::createResult(GameEnum::THIRD,
                (int)$params['hostScoreTGame'], (int)$params['guestScoreTGame']);
            $resultThirdGame->setMatch($match);

            array_push($collectionResults, $resultThirdGame);
        }

        $match->setResults($collectionResults);

        return $match;
    }

    public static function createMatch($id, $date, $playerGuest, $playerHost,
            $stageOfTournament, $winner, $results) {

        $match = new Match($playerHost, $playerGuest);

        if (isset($id)) {
            $match->setId($id);
        }
        if (isset($date)) {
            $match->setDate($date);
        }
        if (isset($playerGuest)) {
            $match->setPlayerGuest($playerGuest);
        }
        if (isset($playerHost)) {
            $match->setPlayerHost($playerHost);
        }
        if (isset($stageOfTournament)) {
            $match->setStageOfTournament($stageOfTournament);
        }
        if (isset($winner)) {
            $match->setWinner($winner);
        }
        if (isset($results)) {
            $match->setResults($results);
        }

        return $match;
    }
}